<template>
	<view class="">
		<heade title="下单" :fanStatus="false" bg_color="bg_fff"></heade>
		<view class="">
			<!-- <view class="bg_fff p-t10 p-b15">
				<view class="headeClass  co_fff f-s20 f-w600 d-flex align-center justify-content-center">您本月还未产生销售，加油哟！</view>
			</view> -->
			<view class="">
				<!-- 常用功能 -->
				<view class="bg_fff p-b20  box_box m-t12 m-l12 m-r12 border-r10">
					<view class="f-s15 p-t20 p-l15">常用功能</view>
					<view class="d-flex align-center m-t20">
						<view class="text-center" style="width: 33.33%;" v-for="(item,i) in commonly" @tap="$common.url(item.url)">
							<text :class="[item.icon,i == 0?'f-s28':'f-s22']" class="iconfont " :style="{'color': item.color}"></text>
							<view class="f-s14 m-t8">{{item.name}}</view>
						</view>
					</view>
				</view>
				<!-- 全部列表 -->
				<view class="bg_fff p-b20  box_box m-t12 m-l12 m-r12 border-r10">
					<view class="f-s15 p-t20 p-l15">全部列表</view>
					<view class="d-flex align-center m-t20">
						<view class="text-center" style="width: 33.33%;" v-for="(item,i) in wholeList" @tap="$common.url(item.url)">
							<text :class="[item.icon,i == 2?'f-s22':'f-s28']" class="iconfont " :style="{'color': item.color}"></text>
							<view class="f-s14 m-t8">{{item.name}}</view>
						</view>
					</view>
				</view>
				<!-- 常用设置 -->
				<view class="bg_fff p-b20  box_box m-t12 m-l12 m-r12 border-r10">
					<view class="f-s15 p-t20 p-l15">常用设置</view>
					<view class="d-flex align-center m-t20">
						<view class="text-center" style="width: 33.33%;" v-for="(item,i) in setUpList" @tap="urlLink(item)">
							<text :class="[item.icon]" class="iconfont f-s24" :style="{'color': item.color}"></text>
							<view class="f-s14 m-t8">{{item.name}}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		openComDB,
		executeSQL,
		dropSQL
	} from '@/common/sqlite.js';
	export default {
		data() {
			return {
				index:0,
				commonly:[
					{name:'销售订单',icon:'icon-xiaoshoudingdan',color:'#ff5c5f',url:'/pages/parameters/parameters?title=销售订单'},
					{name:'销货单',icon:'icon-xierushujuku',color:'#fe8f28',url:'/pages/parameters/parameters?title=销货单'},
					{name:'退货单',icon:'icon-tuihuodan',color:'#fe8f28',url:'/pages/parameters/parameters?title=退货单'},
				],
				wholeList:[
					{name:'销售订单列表',icon:'icon-dingdan',color:'#33b0fa',url:'/pages/salesOrderList/salesOrderList'},
					{name:'销货单列表',icon:'icon-dingdanguanli',color:'#33b0fa',url:'/pages/salesOrderList/salesList'},
					{name:'草稿列表',icon:'icon-caogaoxiang',color:'#06b447',url:'/pages/aDraftList/aDraftList'},
				],
				setUpList:[
					{name:'设置',icon:'icon-shezhi',color:'#33b0fa',url:'/pages/home/setUp'},
					{name:'更新数据',icon:'icon-shujubiangeng',color:'#33b0fa'},
					// {name:'云打印设置',icon:'icon-yundayin',color:'#fd5e63'},
				],
				backButtonPress:0,
				InventoryPrice:[],
				InventoryUnit:[],
				InventoryQueryV2:[],
				CurrentStock:[],
				// #ifdef APP-PLUS
				number:10000,
				// #endif
				// #ifdef H5
				number:200,
				// #endif
			}
		}, 
		onLoad: function(option) {
			this.getWarehouse()
		},
		methods: { 
			isOpenDB: function(data) {
				console.log('是否打开数据库');
				var isOpen = plus.sqlite.isOpenDatabase({
					name: 'xiaoap', //数据库的名字  
					path: '_doc/xiaoap.db' //地址  
				}); 
				console.log(!isOpen);
				// this.getWarehouse()
				if (!isOpen) {
					this.openDB(data); //打開DB  
				} else {
					this.dbInit(data);
				}
			},
 
			openDB: function(data) {
				//SQLite      
				openComDB('xiaoap', '_doc/xiaoap.db', res => {
					console.log('打开数据库');
					this.dbInit(data);
				});
			},
			urlLink(item) {
				if(item.name == '更新数据') {
					
					this.getWarehouse()
				}else {
					this.$common.url(item.url)
				}
			},
			// 获取仓库列表
			getWarehouse() {
				uni.showLoading({
					title: '正在同步数据',
					mask:true 
				});
				this.$http.post('ApiAS/WarehouseQuery',{ "param": {}},true).then(res => {
					if(res.statusCode = 200) {
						uni.hideLoading();
						// this.$db.set('warehouse',JSON.parse(res.data))
						this.isOpenDB(JSON.parse(res.data));
					}else{
						uni.showToast({title:'同步失败请查看网络，请检查网络',icon:'none'})
						uni.hideLoading();
					}
				})
			}, 
			dbInit: function(data) {
				//创建仓库列表；
				let sqlTable = `create table if not exists warehouse
								("ID" INT(10) NOT NULL UNIQUE,
								 "Name" CHAR,
								 "Code" CHAR
								)`
				executeSQL('xiaoap', sqlTable, res => {
					console.log('表创建成功:' + res);
					for(var i=0; i < data.length; i++) {
						let sqlInsert = "insert into warehouse values('";
						sqlInsert +=  data[i].ID+ "','";
						sqlInsert += data[i].Name + "','";
						sqlInsert += data[i].Code + "'";
						sqlInsert += ')';
						executeSQL('xiaoap', sqlInsert, res => {})
					}
					
					executeSQL('xiaoap', 'select * from warehouse', res => {
						// console.log("建筑物查询结果:" + JSON.stringify(res));
						this.getClassQuery()
					})
				})
			},
			getClassQuery() {
				this.$http.post('ApiAS/PartnerClassQuery',{param:{}},true).then(res => {
					if(res.statusCode == 200) {
						// this.$db.set('kehuList',res.data)
						console.log(222)
						this.dbClassQuery(res.data)
						// this.getorder()
					}else{
						uni.showToast({title:'同步失败请查看网络，请检查网络',icon:'none'})
						uni.hideLoading();
					}
				})
			},
			dbClassQuery: function(data) {
				//创建仓库列表；
				let sqlTable_1 = `create table if not exists kehuList
								("id" INT(10) NOT NULL UNIQUE,
								 "code" VARCHAR
								 "name" VARCHAR,
								 "parentCode" VARCHAR,
								 "depth" VARCHAR,
								 "isEndNode" VARCHAR,
								 "ts" VARCHAR,
								 "sub" LONGTEXT
								)`
				executeSQL('xiaoap', sqlTable_1, res => {
					console.log('表创建成功:' + JSON.stringify(res));
					for(var i=0; i < data.length; i++) {
						let sqlInsert_1 = "insert into kehuList values('";
						sqlInsert_1 +=  data[i].id+ "','";
						sqlInsert_1 += data[i].code + "','";
						sqlInsert_1 += data[i].name + "','";
						sqlInsert_1 += data[i].parentCode + "','";
						sqlInsert_1 += data[i].depth + "','";
						sqlInsert_1 += data[i].isEndNode + "','";
						sqlInsert_1 += data[i].ts + "','";
						sqlInsert_1 += JSON.stringify(data[i].sub) + "'";
						sqlInsert_1 += ')';
						console.log(sqlInsert_1)
						executeSQL('xiaoap', sqlInsert_1, res => {})
					}
					
					executeSQL('xiaoap', 'select * from kehuList', res => {
						console.log("建筑物查询结果:" + JSON.stringify(res));
					})
				})
			},
		}
	}
</script>

<style>
	page {
		background: #f3f2f7;
	}
</style>
<style lang="scss">
	.headeClass{
		width: 710rpx;
		height: 190rpx;
		background-color: #4095ff;
		border-radius: 20rpx;
		margin: 0 auto;
	}
</style>
